$(function () {
    //修改
    $('.btn-edit').on('click',function () {
        var that = $(this),
            tr = that.parents('tr');
        var id = tr.find('.user-id').html(),
            mobile = tr.find('.user-mobile').html(),
            name = tr.find('.user-name').html();
        $('#myModal_edit .mobile').val(mobile);
        $('#myModal_edit .name').val(name);
        $('#myModal_edit .sureTap').attr('data-id',id);
    });
    //保存
    $('.sureTap').on('click',function () {
        var that = $(this),
            id = that.attr('data-id'),
            name = that.parents('#myModal_edit').find('.name').val();
        if (!id || !name){
            lightyear.notify('参数不能为空~', 'danger', 1000, 'mdi mdi-emoticon-neutral', 'top', 'center');
            return false;
        }
        var param = {};
        var base = new Base64();
        param.id = base.encode(id);
        param.name = base.encode(name);
        $.post('user_update',param,function (result) {
            if (result.status){
                lightyear.notify(result.msg, 'info', 1000, 'mdi mdi-emoticon-happy', 'top', 'center', 'users.html');
            } else {
                lightyear.notify(result.msg, 'danger', 1000, 'mdi mdi-emoticon-neutral', 'top', 'center');
            }
        },'json');
    });

    //禁用
    $('.btn-status').on('click',function () {
        var that = $(this),
            tr = that.parents('tr'),
            status = that.attr('data-status'),
            id = tr.find('.list-id').html();
        var base = new Base64();
        $.post('user_status',{id:base.encode(id),status:base.encode(status)},function (result) {
            if (result.status){
                lightyear.notify(result.msg, 'info', 1000, 'mdi mdi-emoticon-happy', 'top', 'center', 'users.html');
            } else {
                lightyear.notify(result.msg, 'danger', 1000, 'mdi mdi-emoticon-neutral', 'top', 'center');
            }
        },'json');
    });

    //消费
    $('.btn-payout').on('click',function () {
        var that = $(this),
            tr = that.parents('tr');
        var id = tr.find('.user-id').html(),
            total_cost = tr.find('.user-cost').html(),
            mobile = tr.find('.user-mobile').html(),
            name = tr.find('.user-name').html();
        $('#myModal_payout .mobile').val(mobile);
        $('#myModal_payout .name').val(name);
        $('#myModal_payout .total_cost').val(total_cost);
        $('#myModal_payout .payoutTap').attr('data-id',id);
    });

    //计算余额
    $('#myModal_payout .cost').on('input',function () {
        var that = $(this);
        var cost = that.val();
        var total_cost = $('#myModal_payout .total_cost').val();
        var result_cost = pay_cost(total_cost,cost);
        if (result_cost < 0){
            $('#myModal_payout .payoutTap').attr('disabled',true);
            lightyear.notify('用户余额不足', 'danger', 1000, 'mdi mdi-emoticon-neutral', 'top', 'center');
            return false;
        }
        $('#myModal_payout .result_cost').val(result_cost);
    })

    //计算函数
    function pay_cost(total_cost,cost ) {
        $('#myModal_payout .payoutTap').attr('disabled',false);
        return parseInt(total_cost) - parseInt(cost);
    }

    //消费结算
    $('#myModal_payout .payoutTap').on('click',function () {
        var that = $(this),
            id = that.attr('data-id'),
            cost = that.parents('#myModal_payout').find('.cost').val();
        var param = {};
        var base = new Base64();
        param.id = base.encode(id);
        param.cost = base.encode(cost);
        that.attr('disabled',true);
        $.post('pay_out',param,function (result) {
            if (result.status){
                lightyear.notify(result.msg, 'info', 1000, 'mdi mdi-emoticon-happy', 'top', 'center', 'users.html');
            } else {
                lightyear.notify(result.msg, 'danger', 1000, 'mdi mdi-emoticon-neutral', 'top', 'center');
            }
        },'json');
    });


    //分页
    layui.use('laypage', function() {
        var laypage = layui.laypage;
        var count = $('.page #count').val(); //总条数
        var sort = $('.page #sort').val();   //每页显示条数
        var curr = $('.page #curr').val();   //当前页
        var keyword = $('.keyword').val();  //搜索关键词
        var laypage = layui.laypage;
        //执行一个laypage实例
        laypage.render({
            elem: 'page-box', //注意，这里的 test1 是 ID，不用加 # 号,
            count: count, //数据总数，从服务端得到,
            groups: 8,
            limit: sort,
            curr: curr,
            first: "首页",
            last: '尾页',
            layout: ['prev', 'page', 'next', 'skip'],
            jump: function(obj,first) {
                if(!first){
                    // console.log(obj.curr); //得到当前页，以便向服务端请求对应页的数据。
                    var page = obj.curr
                    if (!keyword){
                        location.href = 'users.html?page='+page;
                    }else {
                        location.href = 'users.html?key='+keyword+'&page='+page;
                    }
                }
            }
        });
    });
});